package com.mall.biz.trade.impl.o2o.incluce;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.biz.trade.O2OTradeBiz;
import com.mall.client.commkey.CommKeyService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.client.mallGroupBuy.MallTuanCodeService;
import com.mall.common.util.CodeUtil;
import com.mall.dto.TradePriceAndBuyCountDTO;
import com.mall.enums.RegionEnum;
import com.mall.pojo.Commkey;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.MallTuanCode;
import com.mall.pojo.Trade;
import com.mall.web.util.MoneyUtil;

@Service
public class TuanGouTradeImpl implements O2OTradeBiz {
	private static final Logger log = LoggerFactory.getLogger(TuanGouTradeImpl.class);
	@Autowired
	private MallGroupbBuyService mallGroupBuyService;
	@Autowired
	private CommKeyService commKeyService;
	@Autowired
	private MallTuanCodeService mallTuanCodeService;

	@Override
	public TradePriceAndBuyCountDTO pack4PirceAndBuyCount(HttpServletRequest request, Integer type, Long price,
			Integer buyCount, Long parentId) {
		TradePriceAndBuyCountDTO tradePriceAndBuyCountDTO = new TradePriceAndBuyCountDTO();
		tradePriceAndBuyCountDTO.setBuyCount(buyCount);
		tradePriceAndBuyCountDTO.setPrice(price);
		MallGroupBuy mallGroupBuy = this.mallGroupBuyService.getMallGroupBuyById(parentId);
		if(mallGroupBuy == null){
			return null; // TODO 跳转到error页
		}
		Long discountPrice = (mallGroupBuy.getOriginalPrice() - mallGroupBuy.getPrice()) * buyCount;
		tradePriceAndBuyCountDTO.setDiscountPrice(discountPrice);
		return tradePriceAndBuyCountDTO;
	}

	

	@Override
	public Boolean beforeInsert(HttpServletRequest request,Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public Boolean afterSuccInsert(HttpServletRequest request, Trade trade) {
		List<MallTuanCode> mallTuanCodeList = new ArrayList<>();
		MallTuanCode mallTuanCode = null;
		for (int i = 0; i < trade.getBuyCount(); i++) {
			mallTuanCode = new MallTuanCode();
			mallTuanCode.setCode(CodeUtil.mallGroupBuyCode(trade.getBuyUserId()));
			mallTuanCode.setCopies(i + 1);
			mallTuanCode.setGmtCreate(new Date());
			mallTuanCode.setGmtModify(new Date());
			mallTuanCode.setMallId(trade.getMallId());
			mallTuanCode.setOutTradeId(trade.getOutTradeNo());
			mallTuanCode.setStatus(0);
			mallTuanCode.setType(0);
			mallTuanCode.setUserId(trade.getBuyUserId());
			mallTuanCode.setTradeId(trade.getId());
			List<Commkey> commKeyList = this.commKeyService.getComKeyByCommOnlyKey(trade.getCommOnlyKey());
			mallTuanCode.setCommOnlyKey(trade.getCommOnlyKey());
			mallTuanCode.setProvinceId(commKeyService.getRegionByList(RegionEnum.PROVINCE.getCode(), commKeyList));
			mallTuanCode.setCityId(commKeyService.getRegionByList(RegionEnum.CITY.getCode(), commKeyList));
			mallTuanCode.setAreaId(commKeyService.getRegionByList(RegionEnum.AREA.getCode(), commKeyList));
			mallTuanCodeList.add(mallTuanCode);

		}
		Long insertTuanCode = this.mallTuanCodeService.insertMallTuanCodeList(mallTuanCodeList);
		if(insertTuanCode<0){
			log.error("生成团购码错误");
			return null; // TODO 跳转到error页
		}
		return true;
	}



	@Override
	public Boolean afterErrorInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

}
